package com.cy.pj.spu.controller;

import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.modules.entity.Spu;
import com.cy.pj.modules.service.ISpuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


/**
 * 商品基本信息Controller
 *
 * @author kiven
 * @date 2023-02-09
 */
@Controller
@RequestMapping("/spu")
public class SpuController
{
    private String prefix = "modules/spu";

    @Autowired
    private ISpuService spuService;

    @RequiresPermissions("spu:view")
    @GetMapping()
    public String spu()
    {
        return prefix + "/spu";
    }


    /**
     * 查询商品基本信息列表
     */
    @RequiresPermissions("spu:list")
    @GetMapping("/list")
    @ResponseBody
    public JsonResult list(Spu spu,Integer pageCurrent)
    {
        // 此分页是自己在BaseController封装的
        return new JsonResult(spuService.selectSpuList(spu,pageCurrent));
    }

    ///**
    // * 导出商品基本信息列表
    // */
    //@RequiresPermissions("spu:export")
    //@PostMapping("/export")
    //@ResponseBody
    //public AjaxResult export(Spu spu)
    //{
    //    List<Spu> list = spuService.selectSpuList(spu);
    //    ExcelUtil<Spu> util = new ExcelUtil<Spu>(Spu.class);
    //    return util.exportExcel(list, "spu");
    //}

    /**
     * 新增商品基本信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品基本信息
     */
    @RequiresPermissions("spu:add")
    @PostMapping("/add")
    @ResponseBody
    public JsonResult addSave(Spu spu)
    {
        // 暂且 商品号代替ID
        spu.setIsHot(spu.getIsHot() != null&&spu.getIsHot()!=0? 1 :0);
        spu.setIsOnSale(spu.getIsOnSale() != null&&spu.getIsOnSale()!=0? 1 :0);
        spu.setIsNew(spu.getIsNew() != null &&spu.getIsNew()!=0?1 :0);
        return new JsonResult(spuService.insertSpu(spu));
    }

    /**
     * 修改商品基本信息
     */
    @GetMapping("/edit")
    public String edit( Long id, ModelMap mmap)
    {
        Spu spu = spuService.selectSpuById(id);
        mmap.put("spu", spu);
        return prefix + "/spu_edit";
    }

    @GetMapping("/id")
    @ResponseBody
    public JsonResult getSpuById(Long id){
        return new JsonResult(spuService.selectSpuById(id));
    }

    /**
     * 修改保存商品基本信息
     */
    @RequiresPermissions("spu:update")
    @PostMapping("/edit")
    @ResponseBody
    public JsonResult editSave(Spu spu)
    {
        // 暂且 商品号代替ID
       spu.setId(Long.parseLong(spu.getGoodsSn()));
        spu.setIsHot(spu.getIsHot() != null&&spu.getIsHot()!=0? 1 :0);
        spu.setIsOnSale(spu.getIsOnSale() != null&&spu.getIsOnSale()!=0? 1 :0);
        spu.setIsNew(spu.getIsNew() != null &&spu.getIsNew()!=0?1 :0);

        return new JsonResult(spuService.updateSpu(spu));
    }

    /**
     * 删除商品基本信息
     */
    @RequiresPermissions("spu:delete")
    @PostMapping( "/remove")
    @ResponseBody
    public JsonResult remove(String ids)
    {
        return new JsonResult(spuService.deleteSpuByIds(ids));
    }
}
